package com.example.dllo.moondog.login;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseActivity;

/**
 * Created by dllo on 16/3/3.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private EditText etUsername,etPassword,etPassAgain;
    private Button btnRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        etUsername = (EditText) findViewById(R.id.et_activity_register_username);
        etPassword = (EditText) findViewById(R.id.et_activity_register_password);
        etPassAgain = (EditText) findViewById(R.id.et_activity_register_again_password);
        btnRegister = (Button) findViewById(R.id.btn_activity_register_register);

        btnRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        final String username = etUsername.getText().toString().trim(); //trim去掉首尾空格
        final String password = etPassword.getText().toString().trim();
        String againPwd = etPassAgain.getText().toString().trim();
        if (username == null || username.length() == 0) {
            showToast("用户名不能为空");
            return;
        } else if (password == null || password.length() == 0) {
            showToast("密码不能为空");
            return;
        } else if (againPwd == null || againPwd.length() == 0) {
            showToast("确认密码不能为空");
            return;
        } else if (!againPwd.equals(password)) {
            showToast("两次密码不一致");
            return;
        }

        if ((username != null || username.length() != 0) && (password != null || password.length() != 0)) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMChatManager.getInstance().createAccountOnServer(username,password);
                        RegisterActivity.this.finish();
                    } catch (EaseMobException e) {
                        int errorCode = e.getErrorCode();
                        if (errorCode == EMError.NONETWORK_ERROR) {
                            showToast("网络异常，请检查网络");
                        }else if(errorCode==EMError.USER_ALREADY_EXISTS){
                            showToast("用户已存在");
                        }else if(errorCode==EMError.UNAUTHORIZED){
                            showToast("注册失败，无权限");
                        } else {
                            showToast("注册失败" + e.getMessage());
                        }
                    }
                }
            }).start();

        }
    }


}
